package io.esirong.base;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import io.esirong.base.mvp.IBasePresenter;
import io.esirong.base.mvp.IBaseView;
import io.esirong.util.ToastUtils;

/**
 * @author 黄嵘才(HuangRc)
 */
public abstract class BaseMvpActivity<P extends IBasePresenter> extends BaseActivity
        implements IBaseView {

    protected P mPresenter;

    protected P presenter() {
        return mPresenter;
    }

    /**
     * 请在Presenter层接受事件
     * 为了使MVP层V层的处理更加集加于View.Event不应再注册了
     * <p>如果你仍需要使用，建议使用BaseActivity
     *
     * @return false 不再注册事件;
     */
    @Override
    protected final boolean regEvent() {
        return false;
    }


    @Override
    public ContextX getContextX() {
        return new ContentXWrapper(this);
    }

    @Override
    public void showMsg(Object tag, String msg) {
        ToastUtils.showToast(this, msg);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建present
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView(this);
        }
    }

    /**
     * 创建Presenter
     */
    protected abstract P createPresenter();
}
